package com.anxin.act.wfEntrustTask.service;

import java.util.List;

import com.anxin.act.task.entity.ViewRuTask;
import com.anxin.act.task.entity.WfPushLog;
import com.anxin.act.task.service.ActDockingService;
import com.anxin.act.task.service.ViewRuTaskService;
import com.anxin.act.task.service.WfPushLogService;
import com.anxin.common.utils.sys.UserUtils;
import com.anxin.framework.redis.MqProducer;
import com.anxin.framework.redis.RedisMq;
import com.anxin.framework.web.service.BaseService;
import com.anxin.common.utils.SecurityUtils;
import com.anxin.sys.system.entity.SysUser;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import com.anxin.act.wfEntrustTask.dao.WfEntrustTaskDao;
import com.anxin.act.wfEntrustTask.entity.WfEntrustTask;
import org.springframework.transaction.annotation.Transactional;

/**
 * 委托代办Service业务层处理
 *
 * @author jbs
 * @date 2021-11-18
 */
@Service
public class WfEntrustTaskService extends BaseService<WfEntrustTaskDao,WfEntrustTask> implements RedisMq {
    @Autowired
    private WfEntrustTaskDao wfEntrustTaskDao;
    @Autowired
    private MqProducer mqProducer;
    @Autowired
    private WfPushLogService wfPushLogService;
    @Autowired
    private ActDockingService actDockingService;
    @Autowired
    private ViewRuTaskService viewRuTaskService;


    /**
     * 查询委托代办
     *
     * @param id 委托代办ID
     * @return 委托代办
     */
    public WfEntrustTask getById(String id){
        return wfEntrustTaskDao.getById(id);
    }

    /**
     * 查询委托代办列表
     *
     * @param wfEntrustTask 委托代办
     * @return 委托代办
     */
    public List<WfEntrustTask> findList(WfEntrustTask wfEntrustTask){
        return wfEntrustTaskDao.findList(wfEntrustTask);
    }

    /**
     * 根据用户ID查询把待办委托给当前用户的用户
     * @return
     */
    public List<String> findEntrustUserName(){
        List<String> userNameList = wfEntrustTaskDao.findEntrustUserName(UserUtils.getUser().getId());
        if (!userNameList.contains(UserUtils.getUserName())){
            userNameList.add(UserUtils.getUserName());
        }
        return userNameList;
    }







    /**
     * 保存委托代办
     *
     * @param wfEntrustTask 委托代办
     * @return 结果
     */
    public int save(WfEntrustTask wfEntrustTask){
        int saveCount = super.save(wfEntrustTask);
        // 发送待办对接数据
        mqProducer.sendMessage(WfEntrustTaskService.class,wfEntrustTask.getId());
        return saveCount;
    }


    /**
     * 删除委托代办信息
     *
     * @param id 委托代办ID
     * @return 结果
     */
    public int deleteById(String id){
        SysUser user = SecurityUtils.getUser();
        int delCount = wfEntrustTaskDao.deleteById(id, user.getId());
        // 发送待办对接数据
        mqProducer.sendMessage(WfEntrustTaskService.class,id);
        return delCount;
    }

    /**
     * 修改用户状态
     * @param wfEntrustTask
     * @return
     */
    @Transactional(readOnly = false)
    public int updateStatus(WfEntrustTask wfEntrustTask){
        wfEntrustTask.preUpdate();
        int count = wfEntrustTaskDao.updateStatus(wfEntrustTask);
        // 发送待办对接数据
        mqProducer.sendMessage(WfEntrustTaskService.class,wfEntrustTask.getId());
        return count;
    }


    /**
     * 推送对接待办
     * @param wfEntrustTask
     */
    @Transactional(readOnly = false)
    public void sendDockingTask(WfEntrustTask wfEntrustTask){
        SysUser sysUser = UserUtils.getUser(wfEntrustTask.getUserId());
        List<ViewRuTask> viewRuTaskList = viewRuTaskService.findListByUserId(sysUser.getUserName());

        SysUser user = UserUtils.getUser(wfEntrustTask.getMandator());
        for (ViewRuTask viewRuTask : viewRuTaskList) {
            WfEntrustTask wfEntrustTaskTemp = wfEntrustTaskDao.getById(wfEntrustTask.getId());
            if (wfEntrustTaskTemp != null && "0".equals(wfEntrustTaskTemp.getStatus()) && "0".equals(wfEntrustTaskTemp.getDelFlag())){
                actDockingService.sendTask(viewRuTask,wfEntrustTask.getUserId(),user.getUserName());
            }else {
                this.delDockingTask(wfEntrustTask);
                return;
            }

        }

    }

    /**
     * 删除对接待办
     * @param wfEntrustTask
     */
    @Transactional(readOnly = false)
    public void delDockingTask(WfEntrustTask wfEntrustTask){
        WfPushLog wfPushLog = new WfPushLog();
        wfPushLog.setStatus("SUCCESS");
        wfPushLog.setActOwner(wfEntrustTask.getUserId());
        wfPushLog.setActAssignee(wfEntrustTask.getMandator());
        List<WfPushLog> wfPushLogList = wfPushLogService.findList(wfPushLog);
        for (WfPushLog pushLog : wfPushLogList) {
            WfEntrustTask wfEntrustTaskTemp = wfEntrustTaskDao.getById(wfEntrustTask.getId());
            // 确定是被删除或者停止的状态
            if (wfEntrustTaskTemp == null || "1".equals(wfEntrustTask.getDelFlag()) || "1".equals(wfEntrustTask.getStatus())){
                actDockingService.delTodoTask(pushLog.getTaskId(),pushLog.getActAssignee());
            }else {
                // 如果处理过程中被打开了，则重新推送
                this.sendDockingTask(wfEntrustTask);
                return;
            }
        }

    }

    @Override
    @Transactional(readOnly = false)
    public boolean onMessage(String message) {
        WfEntrustTask wfEntrustTask = wfEntrustTaskDao.getById(message);
        if ("0".equals(wfEntrustTask.getDelFlag()) && "0".equals(wfEntrustTask.getStatus())){
            this.sendDockingTask(wfEntrustTask);
        }else {
            this.delDockingTask(wfEntrustTask);
        }
        return true;
    }
}
